package org.jeecg.modules.yc.util;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

public class SMS4Util {

    public static void main(String[] args) {
        Security.addProvider(new BouncyCastleProvider());

        String plainText = ("0A0B000000020015002500150025001500250015002500150025001500250015002500150025001500250015" +
                "00250015002500150025001500250015002500150025001500250015002500150025001500250015002500150025001500" +
                "25001500250015002500150025001500250015002500150025001500250015002500150025001500250015002500150025" +
                "00150025001500250015002500150025001500250015002500150025001500250015002500150025001500250015002500" +
                "15002500150025001500250015002500150025001500250015002500150025001500250015002500150025001500250015" +
                "00250015002500000000000000000B0A").replace(" ","");
        System.out.println(plainText.length());
        String keyHex = "0123456789abcdeffedcba9876543210"; // 128-bit key

        try {
            byte[] keyBytes = Hex.decode(keyHex);

            // 创建SM4加密算法实例
            Cipher cipher = Cipher.getInstance("SM4/ECB/NoPadding", "BC");
            SecretKeySpec key = new SecretKeySpec(keyBytes, "SM4");

            // 加密
           /* cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encrypted = cipher.doFinal(plainText.getBytes());
            String encryptedHex = new String(Hex.encode(encrypted));
            System.out.println("Encrypted: " + encryptedHex);*/

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decrypted = cipher.doFinal(Hex.decode("9B A0 7B D7 BC C5 82 0E FA 01 6B F9 6D 38 28 AA 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 98 83 4C 5E 1A B3 37 53 C4 B4 1A DA D6 58 DC 20 44 82 2E 6F C9 7E 2D F5 68 3F E9 E8 F8 03 FC F6".replace(" ","")));
            //String decryptedText = new String(decrypted);
            System.out.println("Decrypted: " + TransformationTool.bytesHexString(decrypted));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static byte[] decrypt(byte[] metadata){
        Security.addProvider(new BouncyCastleProvider());
        String keyHex = "0123456789abcdeffedcba9876543210"; // 128-bit key

        try {
            byte[] keyBytes = Hex.decode(keyHex);

            // 创建SM4加密算法实例
            Cipher cipher = Cipher.getInstance("SM4/ECB/NoPadding", "BC");
            SecretKeySpec key = new SecretKeySpec(keyBytes, "SM4");

            // 加密
           /* cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encrypted = cipher.doFinal(plainText.getBytes());
            String encryptedHex = new String(Hex.encode(encrypted));
            System.out.println("Encrypted: " + encryptedHex);*/

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decrypted = cipher.doFinal(metadata);//String decryptedText = new String(decrypted);
            System.out.println("Decrypted: " + TransformationTool.bytesHexString(decrypted));
            return decrypted;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
